package org.androidpn.activity;

import java.util.Date;

import org.androidpn.HospitalClientApplication;
import org.androidpn.R;
import org.androidpn.http.HttpApi;
import org.androidpn.model.CTalkList;
import org.androidpn.model.CUserInfo;
import org.androidpn.model.NetModel;
import org.androidpn.utils.AppCache;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;
import org.androidpn.utils.ToastUtil;

import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class UserInfoDetailActivity extends Activity implements OnClickListener {

	private ImageView ivHead;
	private TextView tvPhone;
	private TextView tvName;
	private TextView tvAge;
	private TextView tvSex;
	private TextView tvHospitalTextView;
	private TextView tvMajor;
	private TextView tvDescription;
	private TextView tvTag;
	private CUserInfo cUserInfo;
	private CUserInfo myUserInfo;

	private RelativeLayout lyHospital;
	private RelativeLayout lyMarjor;
	private RelativeLayout lyDscription;
	private RelativeLayout lyHead;
	private RelativeLayout lyPhone;
	private RelativeLayout lyName;
	private RelativeLayout lyAge;
	private RelativeLayout lySex;
	private RelativeLayout rlTag;

	private TextView tvAddFriend;
	private ImageView ivBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.userinfo_detail_activity);
		myUserInfo = AppCache.getInstance().getUseInfo();
		cUserInfo = (CUserInfo) getIntent().getSerializableExtra("cuserinfo");
		ivHead = (ImageView) findViewById(R.id.iv_head);
		tvPhone = (TextView) findViewById(R.id.tv_phone);
		tvName = (TextView) findViewById(R.id.tv_name);
		tvAge = (TextView) findViewById(R.id.tv_age);
		tvSex = (TextView) findViewById(R.id.tv_sex);
		ivBack = (ImageView) findViewById(R.id.iv_back);
		tvHospitalTextView = (TextView) findViewById(R.id.tv_hospital);
		tvDescription = (TextView) findViewById(R.id.tv_description);
		tvMajor = (TextView) findViewById(R.id.tv_major);
		lyHospital = (RelativeLayout) findViewById(R.id.ly_hospital);
		lyDscription = (RelativeLayout) findViewById(R.id.ly_description);
		lyMarjor = (RelativeLayout) findViewById(R.id.ly_major);
		lyHead = (RelativeLayout) findViewById(R.id.ly_head);
		lyPhone = (RelativeLayout) findViewById(R.id.ly_telephone);
		lyName = (RelativeLayout) findViewById(R.id.ly_name);
		lyAge = (RelativeLayout) findViewById(R.id.ly_age);
		lySex = (RelativeLayout) findViewById(R.id.ly_sex);
		rlTag = (RelativeLayout) findViewById(R.id.ly_tag);
		tvTag = (TextView) findViewById(R.id.tv_tag);
		tvAddFriend = (TextView) findViewById(R.id.tv_addfriend);
		tvAddFriend.setOnClickListener(this);
		ivBack.setOnClickListener(this);
		initData();
	}

	public void initData() {
		DisplayImageOptions options1 = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).showImageOnFail(R.drawable.default_icon)
				.showImageForEmptyUri(R.drawable.default_icon).showImageOnLoading(R.drawable.default_icon).build();
		if (!TextUtils.isEmpty(cUserInfo.getIcon())) {
			ImageLoader.getInstance().displayImage(ConstanceUtil.HOST_NAME + cUserInfo.getIcon(), ivHead, options1);
		}
		tvPhone.setText(cUserInfo.getTelephone());
		tvName.setText(cUserInfo.getName());
		tvAge.setText(cUserInfo.getAge() + "");
		if (cUserInfo.getSex() == ConstanceUtil.TYPE_SEX_WOMAN) {
			tvSex.setText("女");
		} else {
			tvSex.setText("男");
		}
		if (cUserInfo.getType() == ConstanceUtil.TYPE_YISHENG) {
			tvHospitalTextView.setText(cUserInfo.getHospital());
			tvDescription.setText(cUserInfo.getDescription());
			tvMajor.setText(cUserInfo.getMajor());
			// String tag =new String(cUserInfo.getTag());
			// tag.replace(":", "、");
			tvTag.setText(cUserInfo.getTag());
		} else {
			lyHospital.setVisibility(View.GONE);
			lyMarjor.setVisibility(View.GONE);
			lyDscription.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_addfriend: {
			if (AppCache.getInstance().getUseInfo().getTelephone().contentEquals(cUserInfo.getTelephone())) {
				ToastUtil.showShortToast(HospitalClientApplication.getmInstance(), "不能关注自己！");
				return;
			}
			CommentResponseListener listener = new CommentResponseListener();
			HttpApi.addFriend(AppCache.getInstance().getUseInfo().getTelephone(), cUserInfo.getTelephone(), listener,
					listener);
			break;
		}
		case R.id.iv_back:
			this.finish();
			break;
		}
	}

	class CommentResponseListener
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onResponse(String response) {
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if(netModel.getCode() ==ConstanceUtil.REQUEST_SUCCESS){
			ToastUtil.showShortToast(HospitalClientApplication.getmInstance(), netModel.getContent());
			tvAddFriend.setClickable(false);
			tvAddFriend.setEnabled(false);
//			Intent intent =new Intent(UserInfoDetailActivity.this,TalkingActivity.class);
//			CTalkList ctalklist = new CTalkList();
//			ctalklist.setFriend_icon(cUserInfo.getIcon());
//			ctalklist.setFriend_name(cUserInfo.getName());
//			ctalklist.setFriend_telephone(cUserInfo.getTelephone());
//			ctalklist.setMy_icon(myUserInfo.getIcon());
//			ctalklist.setMy_name(myUserInfo.getName());
//			ctalklist.setMy_telephone(myUserInfo.getTelephone());
//			ctalklist.setTalk_time(new Date());
//			intent.putExtra("ctalklist", ctalklist);
//			UserInfoDetailActivity.this.startActivity(intent);
			UserInfoDetailActivity.this.finish();
			}
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			// TODO Auto-generated method stub
			ToastUtil.showShortToast(HospitalClientApplication.getmInstance(), "网络异常");

		}
	}

}
